home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / msoftapp.zip / DIBVIEW.CPP < prev    next >
C/C++ Source or Header  |  1993-06-01  |  6KB  |  232 lines

  1. // dibview.cpp : implementation of the CDibView class
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and Microsoft
  9. // QuickHelp and/or WinHelp documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. #include "diblook.h"
  15.  
  16. #include "dibdoc.h"
  17. #include "dibview.h"
  18. #include "dibapi.h"
  19. #include "mainfrm.h"
  20.  
  21. #ifdef _DEBUG
  22. #undef THIS_FILE
  23. static char BASED_CODE THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CDibView
  28.  
  29. IMPLEMENT_DYNCREATE(CDibView, CScrollView)
  30.  
  31. BEGIN_MESSAGE_MAP(CDibView, CScrollView)
  32.     //{{AFX_MSG_MAP(CDibView)
  33.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  34.     ON_UPDATE_COMMAND_UI(ID_EDIT_COPY, OnUpdateEditCopy)
  35.     ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
  36.     ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, OnUpdateEditPaste)
  37.     ON_MESSAGE(WM_DOREALIZE, OnDoRealize)
  38.     //}}AFX_MSG_MAP
  39.  
  40.     // Standard printing commands
  41.     ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
  42.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
  43. END_MESSAGE_MAP()
  44.  
  45. /////////////////////////////////////////////////////////////////////////////
  46. // CDibView construction/destruction
  47.  
  48. CDibView::CDibView()
  49. {
  50. }
  51.  
  52. CDibView::~CDibView()
  53. {
  54. }
  55.  
  56. /////////////////////////////////////////////////////////////////////////////
  57. // CDibView drawing
  58.  
  59. void CDibView::OnDraw(CDC* pDC)
  60. {
  61.     CDibDoc* pDoc = GetDocument();
  62.  
  63.     HDIB hDIB = pDoc->GetHDIB();
  64.     if (hDIB != NULL)
  65.     {
  66.         LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB);
  67.         int cxDIB = (int) ::DIBWidth(lpDIB);         // Size of DIB - x
  68.         int cyDIB = (int) ::DIBHeight(lpDIB);        // Size of DIB - y
  69.         ::GlobalUnlock((HGLOBAL) hDIB);
  70.         CRect rcDIB;
  71.         rcDIB.top = rcDIB.left = 0;
  72.         rcDIB.right = cxDIB;
  73.         rcDIB.bottom = cyDIB;
  74.         CRect rcDest;
  75.         if (pDC->IsPrinting())   // printer DC
  76.         {
  77.             // get size of printer page (in pixels)
  78.             int cxPage = pDC->GetDeviceCaps(HORZRES);
  79.             int cyPage = pDC->GetDeviceCaps(VERTRES);
  80.             // get printer pixels per inch
  81.             int cxInch = pDC->GetDeviceCaps(LOGPIXELSX);
  82.             int cyInch = pDC->GetDeviceCaps(LOGPIXELSY);
  83.  
  84.             //
  85.             // Best Fit case -- create a rectangle which preserves
  86.             // the DIB's aspect ratio, and fills the page horizontally.
  87.             //
  88.             // The formula in the "->bottom" field below calculates the Y
  89.             // position of the printed bitmap, based on the size of the
  90.             // bitmap, the width of the page, and the relative size of
  91.             // a printed pixel (cyInch / cxInch).
  92.             //
  93.             rcDest.top = rcDest.left = 0;
  94.             rcDest.bottom = (int)(((double)cyDIB * cxPage * cyInch)
  95.                     / ((double)cxDIB * cxInch));
  96.             rcDest.right = cxPage;
  97.         }
  98.         else   // not printer DC
  99.         {
  100.             rcDest = rcDIB;
  101.         }
  102.         ::PaintDIB(pDC->m_hDC, &rcDest, pDoc->GetHDIB(),
  103.             &rcDIB, pDoc->GetDocPalette());
  104.     }
  105. }
  106.  
  107. /////////////////////////////////////////////////////////////////////////////
  108. // CDibView printing
  109.  
  110. BOOL CDibView::OnPreparePrinting(CPrintInfo* pInfo)
  111. {
  112.     // default preparation
  113.     return DoPreparePrinting(pInfo);
  114. }
  115.  
  116. /////////////////////////////////////////////////////////////////////////////
  117. // CDibView commands
  118.  
  119.  
  120. LRESULT CDibView::OnDoRealize(WPARAM wParam, LPARAM)
  121. {
  122.     ASSERT(wParam != NULL);
  123.     CDibDoc* pDoc = GetDocument();
  124.     if (pDoc->GetHDIB() == NULL)
  125.         return 0L;  // must be a new document
  126.  
  127.     CPalette* pPal = pDoc->GetDocPalette();
  128.     if (pPal != NULL)
  129.     {
  130.         CMainFrame* pAppFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
  131.         ASSERT(pAppFrame->IsKindOf(RUNTIME_CLASS( CMainFrame )));
  132.  
  133.         CClientDC appDC(pAppFrame);
  134.         // All views but one should be a background palette.
  135.         // wParam contains a handle to the active view, so the SelectPalette
  136.         // bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
  137.         CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
  138.  
  139.         if (oldPalette != NULL)
  140.         {
  141.             UINT nColorsChanged = appDC.RealizePalette();
  142.             if (nColorsChanged > 0)
  143.                 pDoc->UpdateAllViews(NULL);
  144.             appDC.SelectPalette(oldPalette, TRUE);
  145.         }
  146.         else
  147.         {
  148.             TRACE0("\tSelectPalette failed in CDibView::OnPaletteChanged\n");
  149.         }
  150.     }
  151.  
  152.     return 0L;
  153. }
  154.  
  155. void CDibView::OnInitialUpdate()
  156. {
  157.     CScrollView::OnInitialUpdate();
  158.     ASSERT(GetDocument() != NULL);
  159.  
  160.     SetScrollSizes(MM_TEXT, GetDocument()->GetDocSize());
  161. }
  162.  
  163.  
  164. void CDibView::OnActivateView(BOOL bActivate, CView* pActivateView,
  165.                     CView* pDeactiveView)
  166. {
  167.     CScrollView::OnActivateView(bActivate, pActivateView, pDeactiveView);
  168.  
  169.     if (bActivate)
  170.     {
  171.         ASSERT(pActivateView == this);
  172.         OnDoRealize((WPARAM)m_hWnd, 0);   // same as SendMessage(WM_DOREALIZE);
  173.     }
  174. }
  175.  
  176. void CDibView::OnEditCopy()
  177. {
  178.     CDibDoc* pDoc = GetDocument();
  179.     // Clean clipboard of contents, and copy the DIB.
  180.  
  181.     if (OpenClipboard())
  182.     {
  183.         BeginWaitCursor();
  184.         EmptyClipboard();
  185.         SetClipboardData (CF_DIB, CopyHandle((HANDLE) pDoc->GetHDIB()) );
  186.         CloseClipboard();
  187.         EndWaitCursor();
  188.     }
  189. }
  190.  
  191.  
  192.  
  193. void CDibView::OnUpdateEditCopy(CCmdUI* pCmdUI)
  194. {
  195.     pCmdUI->Enable(GetDocument()->GetHDIB() != NULL);
  196. }
  197.  
  198.  
  199. void CDibView::OnEditPaste()
  200. {
  201.     HDIB hNewDIB = NULL;
  202.  
  203.     if (OpenClipboard())
  204.     {
  205.         BeginWaitCursor();
  206.  
  207.         hNewDIB = (HDIB) CopyHandle(::GetClipboardData(CF_DIB));
  208.  
  209.         CloseClipboard();
  210.  
  211.         if (hNewDIB != NULL)
  212.         {
  213.             CDibDoc* pDoc = GetDocument();
  214.             pDoc->ReplaceHDIB(hNewDIB); // and free the old DIB
  215.             pDoc->InitDIBData();    // set up new size & palette
  216.             pDoc->SetModifiedFlag(TRUE);
  217.  
  218.             SetScrollSizes(MM_TEXT, pDoc->GetDocSize());
  219.             OnDoRealize((WPARAM)m_hWnd,0);  // realize the new palette
  220.             pDoc->UpdateAllViews(NULL);
  221.         }
  222.         EndWaitCursor();
  223.     }
  224. }
  225.  
  226.  
  227. void CDibView::OnUpdateEditPaste(CCmdUI* pCmdUI)
  228. {
  229.     pCmdUI->Enable(::IsClipboardFormatAvailable(CF_DIB));
  230. }
  231.  
  232.